<?php

class DestaquesModel extends Destaques{
	
	public function insertDestaques($arr){
		try {
			$obj = new Destaques();
			foreach($arr as $chave => $valor){
				if($chave != 'files'){
					$chave = ucfirst($chave);
					$met = 'set'.$chave;
					$obj->$met($valor);
				}
			}
			$db = new Db;
			$db->Insert('destaques',$obj);
			$this->lista = $db->res;
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function updateDestaques($arr){
		try {
			$obj = new Destaques();
			foreach($arr as $chave => $valor){
				if($chave != 'files'){
					$chave = ucfirst($chave);
					$met = 'set'.$chave;
					$obj->$met("'".$valor."'");
				}
			}
			$db = new Db;
			$db->Update('destaques',$obj);
			$this->lista = $db->res;
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function deleteDestaques($id){
		try {
			$db = new Db;
			$db->Delete('destaques',$id);
			$this->lista = $db->res;
		} catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function consultarDestaques($parametros){
		try {
			$campos = array('*');
			$where = 'iddestaques = '.$parametros;
			$order = '';
			$db = new Db;
			$db->Select('destaques',$campos,$where,$order);
			$this->lista = $db->res[0];
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function listarDestaques($parametros){
		try {
			$campos = array('*');
			$where = '';
			$order = 'iddestaques DESC';
			$db = new Db;
			$db->Select('destaques AS d INNER JOIN posts AS p ON p.idposts = d.idposts',$campos,$where,$order);
			$view = new DestaquesView();
			$this->lista = $view->listar($db->res);
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function visualizarDestaques($parametros){
		try {
			$campos = array('*');
			$where = '';
			$order = 'd.posicao ASC';
			$db = new Db;
			$db->Select('destaques AS d INNER JOIN posts AS p ON p.idposts = d.idposts  INNER JOIN imagens AS i ON i.idimagens = p.idimagens',$campos,$where,$order);
			$view = new DestaquesView();
			$this->lista = $view->visualizar($db->res);
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
}
?>